package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;
import io.lkt.commons.dynamic.datasource.annotation.DataSource;
import io.lkt.modules.sys.dao.BArticleInfoDao;
import io.lkt.modules.sys.dao.BVideoInfoDao;
import io.lkt.modules.sys.dao.HeadlineTitleInfoDao;
import io.lkt.modules.sys.dao.SpiderTaskDao;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.BArticleTypeInfoService;
import io.lkt.modules.sys.service.BBrandInfoService;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.service.BVideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


@Service("bVideoInfoService")
public class BVideoInfoServiceImpl extends ServiceImpl<BVideoInfoDao, BArticleInfoEntity> implements BVideoInfoService {
    @Autowired
    BArticleTypeInfoService bArticleTypeInfoService;

    @Autowired
    BBrandInfoService bBrandInfoService;

    @Autowired
    private BUserInfoService bUserInfoService;

    @Autowired
    BVideoInfoDao bVideoInfoDao;

    @Autowired
    SpiderTaskDao spiderTaskDao;


    @Autowired
    HeadlineTitleInfoDao headlineTitleInfoDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BArticleInfoEntity> page = this.page(
                new Query<BArticleInfoEntity>().getPage(params),
                new QueryWrapper<BArticleInfoEntity>().orderByDesc("id")
        );
        for(BArticleInfoEntity bArticleInfoEntity : page.getRecords()){
            if(bArticleInfoEntity.getUserId()!=null) {
                BUserInfoEntity bUserInfo = bUserInfoService.getById(bArticleInfoEntity.getUserId());
                bArticleInfoEntity.setUserName(bUserInfo.getNickName());
            }else{
                bArticleInfoEntity.setUserName("管理员");
            }
            if(bArticleInfoEntity.getArticleTypeId()==null){
                BBrandInfoEntity bBrandInfoEntity=bBrandInfoService.getById(bArticleInfoEntity.getArticleBrandId());
                if(bBrandInfoEntity != null){
                    bArticleInfoEntity.setArticleTypeName(bBrandInfoEntity.getBrandName());
                }
            }else {
                BArticleTypeInfoEntity bArticleTypeInfoEntity = bArticleTypeInfoService.getById(bArticleInfoEntity.getArticleTypeId());
                if (bArticleTypeInfoEntity != null) {
                    bArticleInfoEntity.setArticleTypeName(bArticleTypeInfoEntity.getArticleTypeName());
                }
            }
        }
        return new PageUtils(page);
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {
        return bVideoInfoDao.queryTotal(map);
    }

    @Override
    public List<BArticleInfoEntity> queryList(Map<String, Object> map) {

        List<Integer> brandIds=null;
        List<Integer> typeIds=null;
        if (map.get("articleTypeName").toString().length() > 0) {
            brandIds= bBrandInfoService.queryByNameLike(map.get("articleTypeName").toString());
            typeIds= bArticleTypeInfoService.queryByNameLike(map.get("articleTypeName").toString());
        }

        if(brandIds!=null && brandIds.size()>0) {
            map.put("brandIds", brandIds);
        }
        if(typeIds!=null && typeIds.size()>0) {
            map.put("typeIds", typeIds);
        }

        return bVideoInfoDao.queryList(map);
    }

    @Override
    public Integer titleTotal(String articleTitle,String brandId) {

        return bVideoInfoDao.titleTotal(articleTitle,brandId);
    }

    @DataSource("master")
    public BArticleInfoEntity getById(Serializable id)
    {
        return (BArticleInfoEntity)((BVideoInfoDao)this.baseMapper).selectById(id);
    }

    @DataSource("master")
    public boolean save(BArticleInfoEntity entity)
    {
        return retBool(Integer.valueOf(((BVideoInfoDao)this.baseMapper).insert(entity)));
    }

    @DataSource("master")
    public boolean updateById(BArticleInfoEntity entity)
    {
        return retBool(Integer.valueOf(((BVideoInfoDao)this.baseMapper).updateById(entity)));
    }

    @Override
    public List<HeadlineTitleInfo> HeadlineTitle() {
        return this.headlineTitleInfoDao.selectHeadlineTitle();
    }


}
